package com.xc.ksxt.controller;

import com.alibaba.fastjson.JSON;
import com.xc.common.object.LayuiTableResponse;
import com.xc.common.object.PageInfo;
import com.xc.common.object.ResponseEntity;
import com.xc.common.tool.Helper;
import com.xc.db.tables.GradeYear;
import com.xc.ksxt.entity.LibQuestionEntity;
import com.xc.ksxt.entity.GradeYearEntity;
import com.xc.ksxt.service.GradeYearService;
import com.xc.ksxt.vo.GradeYearVO;
import com.xc.sys.entity.SysUserEntity;
import com.xc.sys.service.SysUserService;
import org.apache.commons.lang3.StringUtils;
import org.jooq.Condition;
import org.jooq.Record;
import org.jooq.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/gradeYear")
@CrossOrigin
public class GradeYearController {

    @Autowired
    private GradeYearService gradeYearService;
    private GradeYear GRADEYEAR = GradeYear.GRADE_YEAR.as("GRADEYEAR");
    @CrossOrigin(origins = "*", allowedHeaders = {"*"}, methods = {RequestMethod.GET})
    @RequestMapping({"/getPageList"})
    public LayuiTableResponse<GradeYearVO> getPageList(HttpServletRequest request) {
        LayuiTableResponse<GradeYearVO> ltr = new LayuiTableResponse<>();
        Condition cond = GRADEYEAR.DEL_FLAG.notEqual(-1);
        String name = request.getParameter("name");
        Integer limit = Integer.valueOf(request.getParameter("limit"));
        Integer page = Integer.valueOf(request.getParameter("page"));
        if (StringUtils.isNotBlank(name)) {
            cond = cond.and(GRADEYEAR.NAME.like("%"+name+"%"));
        }
        Long count = gradeYearService.count(cond);
        ltr.setCount(count);
        PageInfo<GradeYearEntity> pageInfo = new PageInfo<>(limit, page);
        Result<Record> r = gradeYearService.select(cond, pageInfo,GRADEYEAR.NAME.desc());
        List<GradeYearVO> list = r.into(GradeYearVO.class);
        for (GradeYearVO v : list) {
            SysUserEntity u = SysUserService.getFrameCaches(v.getCreateUserId());
            v.setCreateUserName(u==null?"":u.getName());
        }
        ltr.setData(list);
        return ltr;
    }

    @RequestMapping({"/getById"})
    public GradeYearEntity getById(Long id) {
        GradeYearEntity entity = gradeYearService.getEntityById(id);
        return entity;
    }

    @RequestMapping({"/save"})
    public ResponseEntity<GradeYearEntity> save(@RequestBody HashMap<String,String> params, HttpServletRequest request) {
        ResponseEntity<GradeYearEntity> res = new ResponseEntity<>();
        GradeYearEntity gradeYearEntity = Helper.map2Bean(new GradeYearEntity(), params);
        gradeYearService.save(gradeYearEntity);
        return res;
    }
    @RequestMapping({"/updateOrderno"})
    public ResponseEntity<LibQuestionEntity> updateOrderno(@RequestBody HashMap<String,String> params) {
        Long id = Long.valueOf(params.get("id"));
        Integer orderno = Integer.valueOf(params.get("orderno"));
        ResponseEntity<LibQuestionEntity> res = new ResponseEntity<>();
        GradeYearEntity lq = gradeYearService.getEntityById(id);
        lq.setOrderno(orderno);
        gradeYearService.update(lq);
        return res;
    }
    @RequestMapping({"/delete"})
    public LayuiTableResponse<GradeYearEntity> delete(@RequestBody Map<String,String> params) {
        String ids = params.get("ids");
        Long[] idarr = Helper.str2longArr(ids);
        LayuiTableResponse<GradeYearEntity> ltr = new LayuiTableResponse<>();
        for (Long id : idarr) {
            GradeYearEntity m = gradeYearService.getEntityById(id);
            m.setDelFlag(-1);
            gradeYearService.update(m);
        }
        return ltr;
    }

}
